package per.wsj.okhttp

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import per.wsj.commonlib.utils.LogUtil
import per.wsj.httplib.ok.HttpUtils
import per.wsj.okhttp.base.BaseViewModel
import per.wsj.okhttp.base.Chapters
import per.wsj.okhttp.base.Repos

class MainViewModel : BaseViewModel() {

    val chapters = MutableLiveData<List<Chapters>>()
    val test = MutableLiveData<Int>()

    fun getChapters1() {
        val url = "https://wanandroid.com/wxarticle/chapters/json"
        launch {
            HttpUtils.execGet<List<Chapters>>(url) {
                chapters.value = it
            }
        }
    }

    fun getChapters2() {
        val url = "https://wanandroid.com/wxarticle/chapters/json"
        launch {
            val result = HttpUtils.get<List<Chapters>>(url)
            chapters.postValue(result)
        }
    }

    fun getChapters3() {

        launch {
            val url = "https://wanandroid.com/wxarticle/chapters/json"
            val result = HttpUtils.get<List<Chapters>>(url) {
                // 这里是异常：
                LogUtil.e("error: " + it)
            }
            result?.let { chapters.postValue(it) }


//            for (i in 0..10000) {
//                test.postValue(i)
//            }
        }
    }

    fun getGitStarUser() {
        launch {
//            val url = "https://gitstar.com.cn/api_repos"
            val url = "http://192.168.86.31:8000/api_repos"
            val param = HashMap<String, Any>()
            param["page"] = 1
            val result = HttpUtils.post<Repos>(url, param) {
                // 这里是异常：
                LogUtil.e("error: " + it)
            }
            LogUtil.e("result: " + result)
        }
    }
}